package org.emmannuel.virtues.client.panel;

import java.util.ArrayList;

import org.emmannuel.virtues.client.RequesterAsync;
import org.emmannuel.virtues.client.common.TOVirtue;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.VerticalPanel;

public class VirtuePanel extends VerticalPanel {

	RequesterAsync greetingService = null;
	
	ArrayList<TOVirtue> list;
	
	VirtueClickHandlerImpl handler ;
	
	public VirtuePanel(RequesterAsync greetingService, AboutVirtuePanel avp) {
		this.greetingService = greetingService;
		this.handler  = new VirtueClickHandlerImpl(this, avp);
		greetingService.showVirtues(new AsyncCallback<ArrayList<TOVirtue>>() {
			
			@Override
			public void onSuccess(ArrayList<TOVirtue> result) {
				list = result;
				showVirtues();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	private void showVirtues(){
		clear();
		for (TOVirtue virtue : list) {
			Button e = new Button(virtue.getName());
			e.setWidth("150px");
			e.addClickHandler(handler);
			e.addStyleName("virtueButton");
			add(e);
		}
	}
	
	public TOVirtue getVirtueByName(String name){
		if(name.equals(null)) return null;
		for (TOVirtue item : list) {
			if(name.equals(item.getName())){
				return item;
			}
		}
		return null;
	}
	
	
}
